import axios from 'axios';
import msgTip from '@/libs/msgTip';
function checkStatus(response) {
    if (response.status >= 200 && response.status < 300) {
        const { data } = response;
        if(data.httpCode === 200) {
          return data;
        }

        const { msg } = data;
        const msgTips = msg || '系统错误'
        msgTip.error(msgTips);
    }
    const error = new Error(response.statusText);
    error.response = response;
    throw error;

}

export default function request(config = {}) {
    return axios.request(Object.assign(config, { params: { key: new Date().getTime() } }))
        .then(checkStatus)
        .catch((error) => {
            console.log(error)
        });
}

//请求前和请求结束的拦截器
axios.interceptors.request.use(function (config) {
    return config;
}, function (error) {
    return Promise.reject(error);
});

axios.interceptors.response.use(function (response) {
    return response;
}, function (error) {
    return Promise.reject(error);
});
